﻿var AjaxServerUrl = "/Home/As/LoginCommon.aspx";
var BackUrl = "";
var IsAutoLogin = false;
var IsRememberUserName = true;
var IsAgree = false;


///执行登录；
function Login() {
    if ($("#txtLoginName").val().trim() == "") {
        alert("系统提示：请输入用户名！");
        $("#txtLoginName").focus();
        return;
    }
    if ($("#txtLoginPwd").val().trim() == "") {
        alert("系统提示：请输入登录密码！");
        $("#txtLoginPwd").focus();
        return;
    }
    if ($("#txtCode").val().trim() == "") {
        alert("系统提示：请输入验证码！");
        $("#txtCode").focus();
        return;
    }

    var postData = "LoginName=" + $("#txtLoginName").val().trim();
    postData += "&LoginPwd=" + $("#txtLoginPwd").val().trim();
    postData += "&IsRemberUserName=" + IsRememberUserName;
    postData += "&IsAutoLogin=" + IsAutoLogin;
    postData += "&VerifyCode=" + $("#txtCode").val();
    AjaxPostEx2(AjaxServerUrl + "?Action=Login", null, postData, function (ret) {
        if (ret.Result == 1) {
            if (BackUrl == "")
                location.href = "/Home/Default.aspx";
            else
                location.href = BackUrl;
        }
        else {
            alert(ret.Message);
            ChangeVerifyCode();
            $("#txtCode").val("");
            return;
        }
    }
    );
}

//设置记录用户名；
function SetRememberUserName() {
    if ($("#hrefRememberUserName").attr("class") == "on1") {
        $("#hrefRememberUserName").attr("class", "");
        IsRememberUserName = false;
    }
    else {
        $("#hrefRememberUserName").attr("class", "on1")
        IsRememberUserName = true;
    }
}

//设置自动登录；
function SetAutoLogin() {
    if ($("#hrefAutoLogin").attr("class") == "on1") {
        $("#hrefAutoLogin").attr("class", "");
        IsAutoLogin = false;
    }
    else {
        $("#hrefAutoLogin").attr("class", "on1")
        IsAutoLogin = true;
    }
}



//设置是否同意
function ChangeAgree() {
    if ($("#hrefIsAgree").attr("class") == "on2") {
        $("#hrefIsAgree").attr("class", "");
        IsAgree = false;
    }
    else {
        $("#hrefIsAgree").attr("class", "on2")
        IsAgree = true;
    }
}


//检查 注册表单。
function CheckRegisterForm() {
    if ($("#txtUserName").val().trim() == "") {
        alert("系统提示：请输入用户名！");
        $("#txtUserName").focus();
        return false;
    }
    if ($("#txtPassWord").val().trim() == "") {
        alert("系统提示：请输入登录密码！");
        $("#txtPassWord").focus();
        return false;
    }
    if (!(/[A-Za-z]+/.test($("#txtPassWord").val().trim()) && /[0-9]+/.test($("#txtPassWord").val().trim()))) {
        alert("提示：密码不少于8个字符,且至少包含数字和字母！");
        $("#txtPassWord").focus();
        return false;
    }
    if ($("#txtConfirmPassWord").val().trim() == "") {
        alert("系统提示：请输入确认密码！");
        $("#txtConfirmPassWord").focus();
        return false;
    }
    if ($("#txtPassWord").val().trim() != $("#txtConfirmPassWord").val().trim())
    {
        alert("系统提示：两次密码输入不一致！请重新输入！");
        $("#txtConfirmPassWord").focus();
        return false;
    }
    if ($("#txtEmail").val().trim() =="") {
        alert("系统提示：请输入您的邮箱地址！");
        $("#txtEmail").focus();
        return false;
    }
    if (!IsAgree) {
        alert("系统提示：您还没有选择同意用户注册协议！");
        return false;
    }
    return true;
}


///注册账号；
function Register() {
    if (!CheckRegisterForm()) {
        return;
    }
    var postData = "UserName=" + $("#txtUserName").val().trim();
    postData += "&PassWord=" + $("#txtPassWord").val().trim();
    postData += "&Email=" + $("#txtEmail").val().trim();

    AjaxPostEx2(AjaxServerUrl + "?Action=Register", null, postData, function (ret) {
        if (ret.Result == 1) {
            if (BackUrl == "")
                location.href = "/Home/Default.aspx";
            else
                location.href = BackUrl;
        }
        else {
            alert(ret.Message);
            return;
        }
    }
    );

}


///刷新验证码；
function ChangeVerifyCode() {
    $("#spImgCode").html("<img src='/Public/VerifyCode.aspx?tt=" + Math.random() + "' width='74' height='34'/>");
}
